<!DOCTYPE html>
<html>
<script src="../../resources/js-test.js"></script>
<script>
// Ensure that events are handled correctly when an image element is adopted.

jsTestIsAsync = true;

var doc = document.documentElement.appendChild(document.createElement("iframe")).contentDocument;
var adopting = false;
var readyStateChangeEventHandlerTriggeredWhenComplete = false;

doc.open();
doc.onreadystatechange = function() {
    if (doc.readyState == "complete") {
        readyStateChangeEventHandlerTriggeredWhenComplete = true;
        testPassed("readystatechange event handler was executed when complete");
        if (adopting)
            testFailed("script triggered during adoption");
        finishJSTest();
    }
}

var img = doc.appendChild(doc.createElement("img"));
var blobUrl = URL.createObjectURL(new Blob);

var x = new XMLHttpRequest;
x.onload = function() {
    img.src = blobUrl;
}
x.onloadend = function() {
    doc.close();
    URL.revokeObjectURL(blobUrl);
    adopting = true;
    document.adoptNode(img);
    adopting = false;
    if (doc.readyState == "complete" && !readyStateChangeEventHandlerTriggeredWhenComplete) {
        testFailed("readystatechange event was dispatched but the handler was suppressed");
    }
}
x.open("get", "data:text/html,");
x.send();
</script>
</html>
